<template>
  <div class="shop">
    <div class="box">
      <h3>商品信息查询</h3>
      <div class="search">
        <input
          type="text"
          class="search_input"
          placeholder="搜索商品"
          v-model="keyword"
          ref="SSS"
          @keydown.enter="handleSearch"
        />
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
        </el-form-item>
      </div>
      <div class="product-table">
        <table>
          <thead>
            <tr>
              <th>名称</th>
              <th>价格</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="(item, index) in Data" :key="index">
              <td>{{ item.name }}</td>
              <td>{{ item.price }}</td>
              <td
                class="add"
                @click="handleShow(item, $event)"
                :class="{ active: index == showIndex }"
              >
                {{ show }}
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</template>

<script>
// @ is an alias to /src
import { onMounted, ref, defineComponent, computed } from 'vue'
import { useStore } from 'vuex'

export default defineComponent({
  setup() {
    onMounted(() => {
      le()
    })
    let store = useStore()

    const data = ref([
      {
        name: '张曙《算法导论》',
        price: 11,
      },
       {
        name: '许杨《离散数学及其应用》',
        price: 21,
      },
        {
        name: '小肥牛',
        price: 21,
      },
          {
        name: '小龙虾',
        price: 150,
      },
          {
        name: '小Web',
        price: 111,
      },
    ])
    let keyword = ref('')
    const LL = ref()
    const Data = computed(() => {
      if (keyword.value === '') {
        return LL.value
      } else {
        return LL.value.filter((item) => item.name.includes(keyword.value))
      }
    })
    const le = () => {
      LL.value = data.value
    }
    const SSS = ref(null)
    const handleSearch = () => {
      console.log(keyword.value)
    }
    let show = '加入购物车'
    const showIndex = ref(-1)
    // 加入购物车方法
    const handleShow = (l, a) => {
      let Collection = a.currentTarget

      Collection.innerText = '已加入'
      Collection.className = 'active'
      l.state = 1
      store.commit('Collection', l)
    }
    return {
      data,
      Data,
      keyword,
      show,
      SSS,
      handleSearch,
      showIndex,
      handleShow,
    }
  },
})
</script>

<style lang="less" scoped>
.shop {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;

  .box {
    width: 80%;
    max-width: 800px;
    margin: 30px auto;
    background-color: #fff;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    border-radius: 8px;
    padding: 20px;

    h3 {
      height: 40px;
      line-height: 40px;
      margin-bottom: 20px;
      font-size: 24px;
      color: #333;
    }

    .search {
      display: flex;
      align-items: center;
      margin-bottom: 20px;

      .search_input {
        flex: 1;
        outline: none;
        border: 1px solid #ccc;
        height: 40px;
        line-height: 40px;
        box-sizing: border-box;
        text-indent: 1em;
        border-radius: 4px;
        margin-right: 10px;
        font-size: 16px;
      }

      .el-form-item {
        margin-bottom: 0;
      }
    }

    .product-table {
      overflow-x: auto;

      table {
        width: 100%;
        border-collapse: collapse;
        text-align: center;
        cursor: pointer;

        thead {
          background-color: #f5f5f5;

          tr {
            th {
              padding: 12px;
              font-weight: bold;
              color: #333;
            }
          }
        }

        tbody {
          tr {
            td {
              padding: 12px;
              border-bottom: 1px solid #eee;

              &:nth-child(2) {
                width: 60px;
              }
            }

            .add {
              width: 94px;
              color: #007bff;
              cursor: pointer;
            }

            .active {
              background: bisque;
              color: aqua;
            }
          }
        }
      }
    }
  }
}
</style>